package cn.ichiva.tushare.sdk.common;

import java.io.*;
import java.nio.ByteBuffer;

/**
 * 数据转换
 * @Description
 * @auther @author Qp Group
 * @create 2020-04-15 10:59
 */
public class DataUtils {

    public static byte[] toByte(int a) {
        return new byte[] { (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) };
    }

    public static byte[] toByte(short a) {
        return new byte[] { (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF)  };
    }

    public static byte[] toByte(long x) {
        return ByteBuffer.allocate(Long.BYTES).putLong(0, x).array();
    }

    public static byte[] toByte(float x) {
        return toByte(Float.floatToIntBits(x));
    }

    public static byte[] toByte(double x) {
        return toByte(Double.doubleToRawLongBits(x));
    }

    public static byte[] toByte(Serializable x){
        try(ByteArrayOutputStream out = new ByteArrayOutputStream();  ObjectOutputStream objOut = new ObjectOutputStream(out)              ){
            objOut.writeObject(x);
            objOut.flush();
            return out.toByteArray();
        }catch (Exception e){}
        return null;
    }

    public static short toShort(byte[] b) {
        return (short) (b[1] & 0xFF | (b[0] & 0xFF) << 8);
    }

    public static char toChar(byte[] b) {
        return (char) toInt(b);
    }

    public static int toInt(byte[] b) {
        return   b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24;
    }

    public static long toLong(byte[] b) {
        ByteBuffer buffer = ByteBuffer.allocate(8).put(b, 0, b.length);
        buffer.flip();
        return buffer.getLong();
    }

    public static float toFloat(byte[] b) {
        return Float.intBitsToFloat(toInt(b));
    }

    public static double toDouble(byte[] b) {
        return Double.longBitsToDouble(toLong(b));
    }

    public static String toString(byte[] b) throws UnsupportedEncodingException {
        return new String(b,"utf-8");
    }

    public static byte[] toByte(String txt) throws UnsupportedEncodingException {
        return txt.getBytes("utf-8");
    }

    public static Object toObject(byte[] b){
        try(ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b))){
            return in.readObject();
        }catch (Exception e){}
        return null;
    }

    public static String encode(byte[] b) throws UnsupportedEncodingException {
        return new String(b,"ISO-8859-1");
    }

    public static byte[] decode(String s) throws UnsupportedEncodingException {
        return s.getBytes("ISO-8859-1");
    }

    public static long sum(byte[] x){
        long sum = 0;
        for (byte b : x) {
            sum += b;
        }
        return sum;
    }

    public static boolean eq(byte[] bytes, byte[] key) {
        if(bytes.length == key.length){
            for (int i = 0; i < bytes.length; i++) {
                if (key[i] != bytes[i]) {
                    return false;
                }
            }

            return true;
        }
        return false;
    }

    /**
     * 获取hash值
     * @param key
     * @return
     */
    public static int hash(byte[] key) {
        int res = 0;
        for (byte b : key) {
            res += b;
        }
        return res;
    }
}